package com.um.pasantiaum.cliente;

import java.util.ArrayList;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.um.pasantiaum.cliente.SelectProdFragment.AdminProductAdapter;
import com.um.pasantiaum.objetos.Producto;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CarritoFragment extends ListFragment {
	public final static String CLICKED_PRODUCT ="CLICKED_PRODUCT";
	public final static String CLICKED_PRODUCT1 ="CLICKED_PRODUCT1";
	ArrayList<String> precios;
	ArrayList<String> productos;
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		setHasOptionsMenu(true);
		int max=0;
		
		Bundle bundle = this.getArguments();
		ArrayList<String> p1 = new ArrayList<String> ();
		p1.add("Carrito");
		
		productos = bundle.getStringArrayList(CLICKED_PRODUCT);
		for (int i = 0;i< productos.size();i++){
			p1.add(productos.get(i));
		}
		precios = bundle.getStringArrayList(CLICKED_PRODUCT1);
		AdminProductAdapter adapter = new AdminProductAdapter(p1);
		
		
		setListAdapter(adapter);

		

	}
	
	public class AdminProductAdapter extends ArrayAdapter<String> {
		int max=0;
		public AdminProductAdapter(ArrayList<String> prod) {
			super(getActivity(), 0, prod);
				max= prod.size();
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(
						com.um.pasantiaum.R.layout.carrito, null);
				convertView.requestFocus();
			}
			
			String p = getItem(position);
			TextView nameView = (TextView) convertView
					.findViewById(com.um.pasantiaum.R.id.nombreCarrito);
			  
			nameView.setText(p);
			Button b = (Button) convertView.findViewById(com.um.pasantiaum.R.id.botonCarrito);
			if(position!= max-1){
				b.setEnabled(false);	
				b.setVisibility(View.GONE);
			}
			b.setOnClickListener(new OnClickListener() {

			
				public void onClick(View view) {
					ParseObject aux = new ParseObject("Pedidos");
					int a = 0;
					 String h =null;
					 Double b =null;
					for (int i =0;i<precios.size();i++){
						 h = precios.get(i);
						 b= Double.valueOf(h);
						 
					//	Integer b = Integer.parseInt(precios.get(i));
						a = a + b.intValue();
					}
					
					String p = "";
					
					for (int j=0;j<productos.size();j++){
						
						p = p +" " + productos.get(j);
						}
					
					aux.put("usuario", ParseUser.getCurrentUser());
					aux.put("empresa", ParseUser.getCurrentUser());
					aux.put("productos", p);
					aux.put("estado", "pending");
					
					try {
						aux.save();
						Toast toast = Toast.makeText( getContext(), "pedido agregado satisfactoriamente", Toast.LENGTH_LONG);

						    	   toast.show();
						    	   

					} catch (ParseException e) {
						Log.d("Productos", "Error: " + e.getMessage());
					}
				}
					
				
			 });
				return convertView;

}



	}
	

}
